package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.TClass;
import henu.bean.User;
import henu.dao.ITClassDao;
import henu.util.Dhcp;

/**
 * 班级表的DAO接口层
 * @author 刘元炬
 *时间2016年9月13日
 */
public class TClassDaoImpl implements ITClassDao{
	protected QueryRunner queryRunner = null;
	
	public TClassDaoImpl(QueryRunner queryRunner) {
		super();
		this.queryRunner = queryRunner;
	}
	
	@Override
	public int save(TClass tclass) {
		QueryRunner qrunner = new QueryRunner(Dhcp.getDataSource());
		String sql = "insert into tb_class(fClass,fGrade,fMajor,fNumber,fCoach,fMonitor,fMonitorPhone,fMemo) values (?,?,?,?,?,?,?,?)";
		Object[] param = {tclass.getfClass(),tclass.getfGrade(),tclass.getfMajor(),tclass.getfNumber(),tclass.getfCoach(),tclass.getfMonitor(),tclass.getfMonitorPhone(),tclass.getfMemo()};
		int n = 0;
		try {
			n = qrunner.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return n;
	}

	@Override
	public int delete(String fClass) {
		QueryRunner qrunner = new QueryRunner(Dhcp.getDataSource());
		String sql = "delete from tb_class where fClass=?";
		int n = 0;
		try {
			n = qrunner.update(sql,fClass);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int update(String fClass , TClass tclass) {
		QueryRunner qrunner = new QueryRunner(Dhcp.getDataSource());
		System.out.println("在执行更新操作中"+fClass);
		String sql = "update tb_class set fGrade=? ,fMajor=?,fNumber=?,fCoach=?,fMonitor=?,fMonitorPhone=?,fMemo=?  where fClass=?";
		Object[] param = {tclass.getfGrade(),tclass.getfMajor(),tclass.getfNumber(),tclass.getfCoach(),tclass.getfMonitor(),tclass.getfMonitorPhone(),tclass.getfMemo(),fClass};
		int n = 0;
		try {
			n = qrunner.update(sql, param);
			System.out.println(n+"行已经发生变化");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<TClass> seslect(String province, String values) {
		QueryRunner qrunner = new QueryRunner(Dhcp.getDataSource());
		String sql1 = "select * from tb_class where fClass = ?";
		String sql2 = "select * from tb_class where fCoach = ?";
		List<TClass> tclass = null;
		if(province.equals("按班级名称"))
		{
			try {
				tclass = qrunner.query(sql1,values,new BeanListHandler(TClass.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if(province.equals("按辅导员姓名"))
		{
			try {
				tclass = qrunner.query(sql2,values,new BeanListHandler(TClass.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return tclass;
	}

	@Override
	public List<TClass> getAll() {
		QueryRunner qrunner = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_class";
		List<TClass> tclasses = null;
		
		try {
			tclasses = qrunner.query(sql,new BeanListHandler<>(TClass.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tclasses;
	}
	@Override
	public TClass get(String fclass) {
		// TODO Auto-generated method stub
				String sql="select * from tb_class where fClass=?";
				Object param[]={fclass};
				TClass tClass=null;
				try {
					tClass=queryRunner.query(sql, param, new BeanHandler<TClass>(TClass.class));
//					System.out.println("get");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return tClass;
	}

}
